<template>
	<div class="system-onlineuser-container">
		<el-card shadow="hover">
			<el-button v-auth="'system:onlineuser:remove'" type="primary" class="ml10" icon="el-icon-delete" @click="onTabelRowsDel()" :disabled="tableData.selections.length <= 0">下线</el-button>
			<el-button v-auth="'system:onlineuser:clean'" type="danger" class="ml10" icon="el-icon-delete" @click="onTabelRowsClean()">清空</el-button>
			<el-table ref="tableOnlineUserRef" :data="tableData.data" @selection-change="onTableSelectionChange" style="width: 100%" row-key="id">
				<el-table-column type="selection" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column label="登录名称" prop="loginName" min-width="150" show-overflow-tooltip></el-table-column>
				<el-table-column label="用户名称" prop="userName" min-width="150" show-overflow-tooltip></el-table-column>
				<el-table-column label="手机号码" prop="phone" min-width="150" show-overflow-tooltip></el-table-column>
				<el-table-column label="邮箱账号" prop="email" min-width="150" show-overflow-tooltip></el-table-column>
				<el-table-column label="登录地址" prop="loginIp" min-width="100" show-overflow-tooltip></el-table-column>
				<el-table-column label="登录地点" prop="loginAd" min-width="150" show-overflow-tooltip></el-table-column>
				<el-table-column label="浏览器" prop="loginBr" min-width="100" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作系统" prop="loginOs" min-width="100" show-overflow-tooltip></el-table-column>
				<el-table-column label="登录时间" prop="loginTime" width="180" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="80" v-auths="['system:onlineuser:remove']">
					<template #default="scope">
						<el-button v-auth="'system:onlineuser:remove'" type="primary" link @click="onTabelRowDel(scope.row)">下线</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onPageSizeChange"
				@current-change="onPageCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30, 40, 50, 60]"
				v-model:current-page="search.pageNum"
				background
				v-model:page-size="search.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getOnlineUserList, delOnlineUser, cleanOnlineUser } from '/@/api/system/onlineuser/index';

export default {
	name: 'onlineuser',
	setup() {
		const tableOnlineUserRef = ref();
		const state = reactive({
			search:{
				pageNum: 1,
				pageSize: 10
			},
			tableData: {
				selections: [],//选中的行数据
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params: any) => {
			getOnlineUserList(Object.assign(state.search, params)).then((res: any) => {
				if(res.code == 0){
					state.tableData.data = res.data;
					state.tableData.total = res.total;
					tableOnlineUserRef.value.clearSelection();
				}
			});
		}

		//查询
		const onTableSearch = () => {
			loadTableData(state.search);
		}
		//重置
		const onResetSearch = () => {
			state.search.pageNum = 1;
			state.search.pageSize = 10;
			loadTableData(state.search);
		}
		// 监听表格选中行
		const onTableSelectionChange = (selections: any) =>{
			state.tableData.selections = selections;
		};
		// 每页显示数据大小改变
		const onPageSizeChange = (val: number) => {
			state.search.pageSize = val;
			loadTableData(state.search);
		};
		// 页码改变
		const onPageCurrentChange = (val: number) => {
			state.search.pageNum = val;
			loadTableData(state.search);
		};
		// 下线选中行
		const onTabelRowsDel = () => {
			const ids = state.tableData.selections.map((item: any) => item.accessToken);
			if(ids && ids.length > 0){
				ElMessageBox.confirm('确定下线选中的在线用户信息吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
				}).then(() => {
					delOnlineUser(ids).then((res: any) => {
						if(res.code === 0){
							ElMessage.success('下线成功');
							loadTableData({});
						}
					});
				}).catch(() => {});
			}
		};
		// 下线当前行
		const onTabelRowDel = (row: any) => {
			ElMessageBox.confirm('确定下线该条在线用户信息吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delOnlineUser([row.accessToken]).then((res: any) => {
					if(res.code === 0){
						ElMessage.success('下线成功');
						loadTableData({});
					}
				});
			}).catch(() => {});
		};
		// 清空
		const onTabelRowsClean = () =>{
			ElMessageBox.confirm('确定清空全部在线用户信息吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				cleanOnlineUser().then((res: any) => {
					if(res.code === 0){
						ElMessage.success('清空成功');
						loadTableData({});
					}
				})
			});
		}
		// 页面加载时
		onMounted(() => {
			loadTableData({});
		});
		return {
			tableOnlineUserRef,
			onTableSearch,
			onTableSelectionChange,
			onResetSearch,
			onPageSizeChange,
			onPageCurrentChange,
			onTabelRowsDel,
			onTabelRowDel,
			onTabelRowsClean,
			...toRefs(state),
		};
	},
};
</script>